<?php

namespace common\behavior\CommonMessagePushing;

use common\model\EntityModel;
use common\model\TaskModel;
use common\service\BehaviorService;
use common\service\OptionsService;
use common\service\TaskAssembleService;
use common\service\TaskUpstreamService;
use Webman\Stomp\Client;

/**
 * 任务分配行为 消息通知
 */
class TaskAssignBehavior
{
    /**
     * @param $params
     * @return void
     */
    public function run($params)
    {
        /***********  预处理开始  ************/
        $params = BehaviorService::getInstance()->dealParam(
            $params,
            [
                'task' => '',
                'executor' => '',
            ],
            [
                'task' => [],
                'executor' => [],
            ],
            'data',
            ['type' => 'task_assign'],
        );
        /***********  行为判断  ************/
        if ($params === false) {
            return;
        }
        /***********  变量迁移  ************/
        list($task, $executor) = $params;
        if (empty($task) || empty($executor)) {
            return;
        }
        /***********  预处理结束  ************/

        /***********  原逻辑迁移  ************/
        if (empty($task['entity_id']) || !in_array($task['executor_type'], [TaskModel::EXECUTOR_TYPE_INSIDE, TaskModel::EXECUTOR_TYPE_DEFAULT])) {
            return;
        }

        $upstreamDoneStatus = TaskUpstreamService::getInstance()->getUpstreamTaskDoneStatus($task);
        $entity = model(EntityModel::class)->find($task['entity_id']);
        $currentTaskName = TaskAssembleService::getInstance()->getNotifyTaskName($entity, $task);
        $first = '新分配给你的' . $currentTaskName;

        if ($upstreamDoneStatus['flag']) {
            $first .= '上游/关联全部完成,可以开始制作';
        } else {
            $first .= sprintf('上游/关联完成情况%d/%d', $upstreamDoneStatus['total_upstream_task_count'] - ($upstreamDoneStatus['relevant_undone_count'] + $upstreamDoneStatus['unrelevant_undone_count']), $upstreamDoneStatus['total_upstream_task_count']);
        }

        $notifyParam = [
            'first' => '新任务通知',
            'note_text' => $first,
            'remark' => "请及时查看",
            'tenant_id' => $task['tenant_id'],
            'project_id' => $task['project_id'],
            'task_id' => $task['id'],
            'module_code' => in_array(module_by_id($task['entity_module_id'])['code'], ['design', 'level', 'asset']) ? 'asset' : 'shot',
            'message_key' => 'new_task_assigned',
        ];
        $templateId = OptionsService::getInstance()->getTaskRemindCommonTemplateId();

        /***********  发送新的队列  ************/
        Client::send('common_message_pushing', build_queue_data_with_xu([
            'message_generated' => [
                [
                    "param" => $notifyParam,
                    'receiver' => $executor['union_id'],
                    'template_id' => $templateId,
                    'content_type' => 'card_message',
                    'notice_type' => 'message_task',
                    'is_popup' => 1,
                    'message_key' => $notifyParam['message_key'],
                    'project_id' => $task['project_id'],
                ]
            ],
        ]));
        /***********  结束  ************/
    }
}
